import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';

import { CudTableService } from '../../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../../public/serve/remove-table/remove-table.service';
import { PaginationComponent } from '../../../../public/pagination/pagination.component';

import { appProductUrl } from '../../../product.url';
import { product } from '../../../product.data';
import {appSynonymUrl} from '../../../../synonym/synonym.url';
import {Service} from '../../../../server/server';

declare var $: any;

@Component({
    selector: 'app-product-indexing-task-list-audit-persons',
    templateUrl: './product-indexing-task-list-audit-persons.component.html'
})
export class ProductIndexingTaskListAuditPersonsComponent implements OnInit {
    total: number = 0;
    naturalNum: number = 0;
    list: Array<object>;

    queryCriteria: string = '';

    @Input() selectedAuditPersonsNames: string;     //选中的分配人
    @Input() taskName: string;                      //任务名称
    @Output() auditUserNames = new EventEmitter<string>();
    @ViewChild('pagination') pagination: PaginationComponent;

    constructor(
        private server:Service,
        private request: CudTableService,
        private hintsService: HintsService,
        public removeTableService: RemoveTableService) { }


    ngOnInit() {
    }

    getTastAuditList() {
        this._cancelChecked();
        let params = this.getTastAuditListParams();
        this.request.postRequest(this.server.baseUrl +appProductUrl.getPusersList, params).then((res: any) => {
            if (res.result_code === '0000') {
                this.list = res.result;
                this.total = res.length;
                this.naturalNum = product.pagination.naturalNum;
                //选中原先的
                if (this.selectedAuditPersonsNames) {
                    for (let item of this.list) {
                        if (item['userCode'] === this.selectedAuditPersonsNames) {
                            item['isChecked'] = true;
                            this.removeTableService.removeChose(true, item, this.list.length)['bool'];
                        }
                    }
                }
            } else {
                this.hintsService.messages(res.message, res.result_code);
            }
            return;
        })
            .catch((err: any) => {
                this.hintsService.messages("error", "error");
                return;
            });
    }

    getTastAuditListParams() {
        return {
            token: product.retrieval.token,
            pageNumber: product.pagination.pageNumber,
            pageSize: product.pagination.pageSize,
            id: product.retrieval.id,
            queryCriteria: this.queryCriteria,
            queryType: 1,
            startCreateTime: '',
            endCreateTime: '',
            endLoginTime: ''
        }
    }

    /**
     * 单选
     * @param child
     * @param length
     */
    _chose(child, length) {
        this.selectedAuditPersonsNames = child.userCode;
        this.removeTableService.removeAll(false, '', '');
        this.removeTableService.removeChose(true, child, length)['bool'];
    }

    /**
     * 重置表格单选框、选中的ID
     */
    private _cancelChecked() {
        this.list = [];
    }

    clearQueryCriteria() {
        this.queryCriteria = '';
        this.pagination.currentPage = 1;
        this.pagination.pageSize = 20;
        product.pagination.pageNumber = 1;
        product.pagination.pageSize = 20;
        product.pagination.naturalNum = 0;
        this.removeTableService.removeAll(false, '', '');
    }

    /**
     * 关闭弹出窗口
     */
    public dismissModal() {
        this.auditUserNames.emit(this.selectedAuditPersonsNames);
        $("#auditPersons").modal('hide');
    }

    /**
     * 搜索
     * @param val
     */
    _retrieval(val: string) {
        product.retrieval.queryCriteria = val.trim();
        this.queryCriteria = val.trim();
        this.getTastAuditList();
    }
    /**
      * 分页
      * @param pagination 
      */
    _pagination(pagination: Array<any>) {
        product.pagination.pageNumber = pagination[0].pageNumber;
        product.pagination.pageSize = pagination[0].pageSize;
        product.pagination.naturalNum = (product.pagination.pageNumber - 1) * product.pagination.pageSize;
        this.getTastAuditList();
    }
}

